package br.com.controller;

import java.io.IOException;
import java.net.URL;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import br.com.model.Permissao;
import br.com.model.Usuario;
import br.com.persistence.UsuarioDao;

@ManagedBean(name = "login", eager = true)
@SessionScoped
public class Login {

	private Usuario usuario;
	private String ErrorMessage;

	public Login() {
		this.usuario = new Usuario();
		this.ErrorMessage = "";
	}

	public void validaLogin(ActionEvent action) {
		UsuarioDao dao = new UsuarioDao();
		dao.selectOne(usuario);

		FacesContext faces = FacesContext.getCurrentInstance();

		// selectOne retorna true se encontrar o usuario
		if (dao.selectOne(usuario)) {
			// setErrorMessage(Boolean.toString(dao.selectOne(usuario)));
			try {
				int permissao = usuario.getPermissao().getIdPermissao();

				switch (permissao) {
				case Permissao.ADMIN:
					faces.getExternalContext().redirect(
							"view/admin/inicio.xhtml");
					break;
				case Permissao.PROFESSOR:
					faces.getExternalContext().redirect(
							"view/professor/inicio.xhtml");
					break;
				case Permissao.ALUNO:
					faces.getExternalContext().redirect(
							"view/aluno/inicio.xhtml");
					break;
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			// cria um alert do jsf com a mensagem abaixo
			faces.addMessage("frmLogin:btnLogin", new FacesMessage(
					"Erro de login", "Matricula ou senha incorretos!"));

			ErrorMessage = "Matricula ou senha incorretos!";
			try {
				faces.getExternalContext().redirect("index.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public void logout(ActionEvent action) {
		try {
			System.out.println("logout");
			// apaga os dados do usuario atual
			usuario = new Usuario();

			FacesContext faces = FacesContext.getCurrentInstance();

			HttpServletRequest req = (HttpServletRequest) faces
					.getExternalContext().getRequest();

			URL url = new URL("http", req.getServerName(), req.getServerPort(),"/web/index.xhtml");
			faces.getExternalContext().redirect(url.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	// retorna a mensagem e limpa o atributo errorMessage
	public String getErrorMessage() {
		String res = ErrorMessage;
		ErrorMessage = "";
		return res;
	}

	public void setErrorMessage(String errorMessage) {
		ErrorMessage = errorMessage;
	}

	public static void main(String[] args) {
		Login login = new Login();
		Usuario user = login.getUsuario();
		user.setIdUsuario(1);
		user.setSenha("1234");
		user.setNome("gilson1");
		user.setCpf("1111011111");
		user.setPermissao(new Permissao(1));

		UsuarioDao dao = new UsuarioDao();

		System.out.println(dao.update(user));
	}

}
